package com.berchen.utils;

import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * 获取spring IOC容器
 * web项目才这样写
 */
@Component
public class SpringContextUtils2 implements ServletContextListener {

    private ServletContext servletContext;

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        this.servletContext=sce.getServletContext();
    }
    public ApplicationContext getIoc(){
        return WebApplicationContextUtils.getWebApplicationContext(this.servletContext);
    }
}
